package com.dev.dream.libs.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.dev.dream.libs.adapter.app.AppController;
import com.dev.dream.libs.adapter.model.AppAdsItem;
import com.dev.dream.libs.mylib.R;

public class MySimpleArrayAdapter extends BaseAdapter {

	private final Context context;
	private ArrayList<AppAdsItem> appAdsItem;
	// private final BitmapCache mMemoryCache;
	ImageLoader imageLoader = AppController.getInstance().getImageLoader();

	// = new int[] { R.drawable.ic_s5clock_64,
	// R.drawable.ic_rainforest_64, R.drawable.ic_winter_64,
	// R.drawable.ic_autumn_64, R.drawable.iconfirefly64,
	// R.drawable.icon64rainclock };

	public MySimpleArrayAdapter(Context context,
			ArrayList<AppAdsItem> appAdsItem) {
		this.context = context;
		this.appAdsItem = appAdsItem;
	}

	@Override
	public int getCount() {
		return appAdsItem.size();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private class ViewHolder {
		NetworkImageView imgIcon;
		TextView txtTitle;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// LayoutInflater inflater = (LayoutInflater) context
		// .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// View rowView = inflater.inflate(R.layout.customlistview, parent,
		// false);
		// TextView textView = (TextView) rowView.findViewById(R.id.txt);
		//
		// textView.setSelected(true);
		// textView.setTextColor(Color.WHITE);
		// ImageView imageView = (ImageView) rowView.findViewById(R.id.img);

		ViewHolder holder = null;
		if (imageLoader == null)
			imageLoader = AppController.getInstance().getImageLoader();

		if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater) context
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.customlistview, null);

			holder = new ViewHolder();

			holder.imgIcon = (NetworkImageView) convertView
					.findViewById(R.id.img);
			holder.txtTitle = (TextView) convertView.findViewById(R.id.txt);
			holder.txtTitle.setSelected(true);
			holder.txtTitle.setTextColor(Color.WHITE);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}

		holder.txtTitle.setText(appAdsItem.get(position).getName());
		holder.txtTitle.setSelected(true);

		if (holder.imgIcon != null) {

			holder.imgIcon.setImageUrl(appAdsItem.get(position).getUrlIcon(),
					imageLoader);

		}

		return convertView;
	}

	@Override
	public boolean isEnabled(int position) {
		return true;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}
}
